import {Router, oakHelpers} from '../../deps.ts'
import {makeArithmeticItems, makeParams} from '../../service/arithmeticService.ts'
const router = new Router();

router
  .get('/pre/arithmetic', async (ctx) => {
    const queryParams = oakHelpers.getQuery(ctx)
    const withAnswer = queryParams["withAnswer"] === 'false' ? false: true
    const take = parseInt(queryParams["take"])
    const seed = parseInt(queryParams["seed"])
    console.log(`query=>${JSON.stringify(queryParams)}`)
    const arithmeticItems = await makeArithmeticItems({withAnswer, take, seed})
    console.log('arithmeticItems=>', arithmeticItems)
    ctx.render("pre-arithmetic", { title: "学前加减法", arithmeticItems, take, seed });
  })
  .post("/pre/arithmetic/make", async (ctx) => {
    console.log("生成加减法");
    const body = await ctx.request.body();
    console.log(`body=>${JSON.stringify(body.value)}`)
    const p = body.value as unknown as makeParams
    const arithmeticItems = await makeArithmeticItems(p)
    console.log('arithmeticItems=>', arithmeticItems)
    
    ctx.render("pre-arithmetic", { title: "学前加减法", arithmeticItems, take: p.take, seed: p.seed });
    // ctx.response.body = {success: true, data: arithmeticItems};
    // ctx.response.type = "json";
  })

export default router